package com.css.fxfzypg.manage.building.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.manage.building.repository.BuildingReqository;
import com.css.fxfzypg.manage.building.repository.entity.BuildingEntity;
import com.css.fxfzypg.manage.building.service.BuildingService;
import com.css.fxfzypg.manage.building.vo.BuildingParams;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.PlatformSessionUtils;
import com.css.fxfzypg.util.UUIDGenerator;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class BuildingServiceImpl implements BuildingService {

    @Autowired
    BuildingReqository buildingReqository;

    @Autowired
    SysAreaService sysAreaService;

    @Override
    public boolean saveBuilding(BuildingEntity buildingEntity) throws UnsupportedEncodingException {
        String uuid = UUIDGenerator.getUUID();
        buildingEntity.setId(uuid);
        /*String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(buildingEntity.getProvince(), "UTF-8"), URLEncoder.encode(buildingEntity.getCity(), "UTF-8"), URLEncoder.encode(buildingEntity.getCounty(), "UTF-8"));
        buildingEntity.setProvinceId(divisionIds.split("_")[0]);
        buildingEntity.setCityId(divisionIds.split("_")[1]);
        buildingEntity.setCountyId(divisionIds.split("_")[2]);*/
        buildingEntity.setCreateTime(new Date());
        buildingEntity.setIsSubmit("0");
        buildingEntity.setDelFlag("0");
        return buildingReqository.saveBuilding(buildingEntity);
    }

    @Override
    public boolean saveAndSubmit(BuildingEntity buildingEntity) throws UnsupportedEncodingException {
        String uuid = UUIDGenerator.getUUID();
        buildingEntity.setId(uuid);
      /*  String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(buildingEntity.getProvince(), "UTF-8"), URLEncoder.encode(buildingEntity.getCity(), "UTF-8"), URLEncoder.encode(buildingEntity.getCounty(), "UTF-8"));
        buildingEntity.setProvinceId(divisionIds.split("_")[0]);
        buildingEntity.setCityId(divisionIds.split("_")[1]);
        buildingEntity.setCountyId(divisionIds.split("_")[2]);*/
        buildingEntity.setCreateTime(new Date());
        buildingEntity.setSubmitter(buildingEntity.getCreateName());
        buildingEntity.setSubmitterId(buildingEntity.getCreateId());
        buildingEntity.setSubmitTime(new Date());
        buildingEntity.setIsSubmit("1");
        buildingEntity.setDelFlag("0");
        return buildingReqository.saveBuilding(buildingEntity);
    }


    @Override
    public Map<String,Object> querySubmitOrNot(String submitFlag, String taskCode, int curPage, int pageSize, String userId) {
        return buildingReqository.querySubmitOrNot(submitFlag,taskCode,curPage,pageSize,userId);
    }

    @Override
    public boolean updateIsSubmit(Map<String,String> map) {
        return buildingReqository.updateIsSubmit(map);
    }

    @Override
    public BuildingEntity queryData(String id) {
        return buildingReqository.queryData(id);
    }

    @Override
    public boolean modifyData(BuildingEntity buildingEntity) {
        return buildingReqository.modifyData(buildingEntity);
    }

    @Override
    public boolean modifyAndSubmit(BuildingEntity buildingEntity) {
        buildingEntity.setIsSubmit("1");
        buildingEntity.setSubmitter(buildingEntity.getCreateName());
        buildingEntity.setSubmitterId(buildingEntity.getCreateId());
        buildingEntity.setSubmitTime(new Date());
        return buildingReqository.modifyData(buildingEntity);
    }

    @Override
    public Map<String, Object> queryBuildingData(HttpServletRequest request, BuildingParams queryParams, int curPage, int pageSize) {
        Map<String, Object> map= buildingReqository.queryBuildingData(request,queryParams,curPage,pageSize);
        return map;
    }

    @Override
    public boolean deleteBuilding(String ids) {
        boolean res = true;
        for(String id : ids.split(",")){
            res = buildingReqository.deleteBuilding(id);
            if(res == false){
               break;
            }
        }
        return res;
    }



}
